package com.plan.base.util;

import com.alibaba.fastjson2.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class HttpUtil {

    public static <T> T sendHttpPostRequestWithInnerProcessResultResponse(String url, Object request, Type responseType, Map<String, String> headerMap) {
        T result = null;
        String requestString = JSON.toJSONString(request);
        String responseString = null;
        CloseableHttpClient httpclient = null;
        CloseableHttpResponse httpResponse = null;

        try {
            httpclient = HttpClients.createDefault();
            HttpPost post = new HttpPost(url);
            StringEntity enity = new StringEntity(requestString, "UTF-8");
            enity.setContentType("application/json");
            post.setEntity(enity);
            if (headerMap != null) {
                Iterator var11 = headerMap.entrySet().iterator();

                while (var11.hasNext()) {
                    Map.Entry<String, String> entry = (Map.Entry) var11.next();
                    post.addHeader((String) entry.getKey(), (String) entry.getValue());
                }
            }

            httpResponse = httpclient.execute(post);
            HttpEntity entity = httpResponse.getEntity();
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                responseString = EntityUtils.toString(entity);
                result = JSON.parseObject(responseString, responseType);
            }
        } catch (Exception var25) {
            var25.printStackTrace();
        } finally {
            if (httpResponse != null) {
                try {
                    httpResponse.close();
                } catch (IOException var24) {
                    var24.printStackTrace();
                }
            }

            if (httpclient != null) {
                try {
                    httpclient.close();
                } catch (IOException var23) {
                    var23.printStackTrace();
                }
            }

        }

        return result;
    }

    public static <T> T sendHttpPostRequest(String url, Object request, Class<T> responseType, Map<String, String> headerMap) {
        T result = null;
        String requestString = JSON.toJSONString(request);
        String responseString = null;
        CloseableHttpClient httpclient = null;
        CloseableHttpResponse httpResponse = null;

        try {
            httpclient = HttpClients.createDefault();
            HttpPost post = new HttpPost(url);
            StringEntity enity = new StringEntity(requestString, "UTF-8");
            enity.setContentType("application/json");
            post.setEntity(enity);
            if (headerMap != null) {
                Iterator var11 = headerMap.entrySet().iterator();

                while (var11.hasNext()) {
                    Map.Entry<String, String> entry = (Map.Entry) var11.next();
                    post.addHeader((String) entry.getKey(), (String) entry.getValue());
                }
            }

            httpResponse = httpclient.execute(post);
            HttpEntity entity = httpResponse.getEntity();
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                responseString = EntityUtils.toString(entity);
                result = JSON.parseObject(responseString, responseType);
            }
        } catch (Exception var25) {
            var25.printStackTrace();
        } finally {
            if (httpResponse != null) {
                try {
                    httpResponse.close();
                } catch (IOException var24) {
                    var24.printStackTrace();
                }
            }

            if (httpclient != null) {
                try {
                    httpclient.close();
                } catch (IOException var23) {
                    var23.printStackTrace();
                }
            }

        }

        return result;
    }

    public static <T> T sendHttpGetRequest(String urlString, Class<T> responseType, Map<String, String> headerMap) {
        T result = null;
        String responseString = null;
        CloseableHttpClient httpclient = null;
        CloseableHttpResponse httpResponse = null;

        try {
            httpclient = HttpClients.createDefault();
            HttpGet get = new HttpGet(urlString);
            if (headerMap != null) {
                Iterator var8 = headerMap.entrySet().iterator();

                while (var8.hasNext()) {
                    Map.Entry<String, String> entry = (Map.Entry) var8.next();
                    get.addHeader((String) entry.getKey(), (String) entry.getValue());
                }
            }

            httpResponse = httpclient.execute(get);
            HttpEntity entity = httpResponse.getEntity();
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                responseString = EntityUtils.toString(entity);
                result = JSON.parseObject(responseString, responseType);
            }
        } catch (Exception var22) {
            var22.printStackTrace();
        } finally {
            if (httpResponse != null) {
                try {
                    httpResponse.close();
                } catch (IOException var21) {
                    var21.printStackTrace();
                }
            }

            if (httpclient != null) {
                try {
                    httpclient.close();
                } catch (IOException var20) {
                    var20.printStackTrace();
                }
            }

        }

        return result;
    }

    public static <T> T sendHttpGetRequest(String urlString, Type responseType, Map<String, String> headerMap) {
        T result = null;
        String responseString = null;
        CloseableHttpClient httpclient = null;
        CloseableHttpResponse httpResponse = null;

        try {
            httpclient = HttpClients.createDefault();
            HttpGet get = new HttpGet(urlString);
            if (headerMap != null) {
                Iterator var8 = headerMap.entrySet().iterator();

                while (var8.hasNext()) {
                    Map.Entry<String, String> entry = (Map.Entry) var8.next();
                    get.addHeader((String) entry.getKey(), (String) entry.getValue());
                }
            }

            httpResponse = httpclient.execute(get);
            HttpEntity entity = httpResponse.getEntity();
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                responseString = EntityUtils.toString(entity);
                result = JSON.parseObject(responseString, responseType);
            }
        } catch (Exception var22) {
            var22.printStackTrace();
        } finally {
            if (httpResponse != null) {
                try {
                    httpResponse.close();
                } catch (IOException var21) {
                    var21.printStackTrace();
                }
            }

            if (httpclient != null) {
                try {
                    httpclient.close();
                } catch (IOException var20) {
                    var20.printStackTrace();
                }
            }

        }

        return result;
    }

    public static String sendHttpGetRequest(String urlString) {
        String responseString = null;
        CloseableHttpClient httpclient = null;
        CloseableHttpResponse httpResponse = null;

        try {
            HttpGet get = new HttpGet(urlString);
            httpclient = HttpClients.createDefault();
            httpResponse = httpclient.execute(get);
            HttpEntity entity = httpResponse.getEntity();
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                responseString = EntityUtils.toString(entity);
            }
        } catch (Exception var19) {
            var19.printStackTrace();
        } finally {
            if (httpResponse != null) {
                try {
                    httpResponse.close();
                } catch (IOException var18) {
                    var18.printStackTrace();
                }
            }

            if (httpclient != null) {
                try {
                    httpclient.close();
                } catch (IOException var17) {
                    var17.printStackTrace();
                }
            }

        }

        return responseString;
    }

    public static <T> T sendHttpPostRequest(String url, List<NameValuePair> params, Type responseType, Map<String, String> headerMap) {
        T result = null;
        String responseString;
        CloseableHttpClient httpclient = null;
        CloseableHttpResponse httpResponse = null;

        try {
            httpclient = HttpClients.createDefault();
            HttpPost post = new HttpPost(url);
            if (headerMap != null) {
                Iterator var9 = headerMap.entrySet().iterator();

                while (var9.hasNext()) {
                    Map.Entry<String, String> entry = (Map.Entry) var9.next();
                    post.addHeader((String) entry.getKey(), (String) entry.getValue());
                }
            }

            post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
            httpResponse = httpclient.execute(post);
            HttpEntity entity = httpResponse.getEntity();
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                responseString = EntityUtils.toString(entity);
                result = JSON.parseObject(responseString, responseType);
            }
        } catch (Exception var23) {
            var23.printStackTrace();
        } finally {
            if (httpResponse != null) {
                try {
                    httpResponse.close();
                } catch (IOException var22) {
                    var22.printStackTrace();
                }
            }

            if (httpclient != null) {
                try {
                    httpclient.close();
                } catch (IOException var21) {
                    var21.printStackTrace();
                }
            }

        }

        return result;
    }

}
